var shopUnique;//商铺唯一性标识
var saleListUnique;//订单唯一性标识
var shop_unique;//商品唯一性标识

$(document).ready(function() {
	var thisURL = document.URL;  
	var  getval =thisURL.split('?')[1].split('&'); 
	saleListUnique=getval[0].split("=")[1];
	saleListTotal=getCookie("saleListTotal");
	$("#saleListUnique").text(saleListUnique);
	$("#saleListTotal").text(saleListTotal);
});


/*
 * 根据商品id查询出对应的商品信息 goodId 商品的id
 */
function goodsdetails(goodsId) {
	$.ajax({
		url : "../goods/findGoodsById.do",
		type : "post",
		data : {
			"goodsId" : goodsId
		},
		dataType : "json",
		success : function(result) {
			if (result.status == 1) {// 查询成功
				console.log(result.data);
				var data = result.data;
				$("#goodsBarcode").text(data.goods_barcode);
				$("#detailsImg").attr("src", data.goods_picturepath );//商品图片显示
				$("#confirm-goodsName").text(data.goods_name);//商品名称
				$("#detailsNumber").text(data.goods_barcode);
				$("#detailsBrands").text(data.stock_avail_count);
				$("#confirm-goodsPrice").text(data.goods_sale_price);//商品价格
				$("#shopUnique").text(data.shop_unique);//商铺的唯一性标识
				
				$("#goods-sold").text("销量:"+data.goods_sold);//销量
				
			} else {
			}
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {

		}
	});
}

//数量增加
function confirmAdd(){
	var price =$("#confirm-goodsPrice").text();
	var count=$("#goodsCount").text();
	var totalPrice=$("#totalPrice").text();
	$("#goodsCount").text(++count);
	$("#totalCount").text(count);
	$("#totalPrice").text(count*price);
	console.log(price+";"+count+";"+totalPrice);
	
}

//数量减少
function confirmSub(){
	var price =$("#confirm-goodsPrice").text();
	var count=$("#goodsCount").text();
	var totalPrice=$("#totalPrice").text();
	if(count>1){
		$("#goodsCount").text(--count);
		$("#totalCount").text(count);
		$("#totalPrice").text(count*price);
	}
	
}


// 确认支付
function submitPay() {
	// 根据订单的标识查询出店铺标识等信息
	var shopUnique;
	var cusUnique;
	$.ajax({
		url : "../online/findSaleById.do",
		type : "post",
		data : {
			"saleListUnique" : saleListUnique
		},
		dataType : "json",
		success : function(result) {
			if (result.status == 1) {// 查询成功
				shopUnique = result.data.shop_unique;
				cusUnique=result.data.cus_unique;
				var saleListPayment = $('input[name="sex"]:checked').val();// 选择的支付方式
				var payMoney = result.data.sale_list_total;// 付款金额总价
				
				var shoppingCartId = getCookie("basketIds");// 购物车id
				var goodsCount = getCookie("goodsCount");// 商品的数量
				var goodsBarcodes = getCookie("goodsBarcode");// 商品的条形码
				console.log(saleListPayment + ";" + payMoney + ";" + shopUnique
						+ ";" + cusUnique + ";" + shoppingCartId + ";"
						+ goodsCount + ";goodsBarcodes:" + goodsBarcodes);
				
			

				if (saleListPayment == 1) {// 支付宝支付
					return;
					
					parent.location.href = "../payOnline/pay.do?payMoney="
							+ payMoney + "&saleListAddress=" + "119"
							+ "&" + "cusUnique=" + cusUnique + "&shopUnique="
							+ shopUnique + "&saleListUnique=" + saleListUnique
							+ "&shoppingCartId=" + shoppingCartId
							+ "&goodsCount=" + goodsCount + "&goodsBarcodes="
							+ goodsBarcodes;

				} else if (saleListPayment == 2) {// 微信支付
					return;
				
				} else if (saleListPayment == 3) {// 货到付款
					
					$.ajax({
						url : "../payOnline/cashPay.do",
						type : "post",
						data : {
							"payMoney" : payMoney,
							"saleListAddress":"119",
							"cusUnique" : cusUnique,
							"shopUnique" : shopUnique,
							"saleListUnique" : saleListUnique,
							"shoppingCartId" : shoppingCartId,
							"goodsCount" : goodsCount,
							"goodsBarcodes" : goodsBarcodes
						},
						dataType : "json",
						success : function(result) {
							if (result.status == 1) {// 查询成功
								parent.location.href = "pay_finish.html";
							} else {
								$('#alert-error').text('网络异常!');
								$('#alert-error').slideToggle(100);
								setTimeout(function() {
									$('#alert-error').slideUp(200);
								}, 2000);
							}
						},
						error : function(XMLHttpRequest, textStatus,
								errorThrown) {
							$('#alert-error').text('网络异常!');
							$('#alert-error').slideToggle(100);
							setTimeout(function() {
								$('#alert-error').slideUp(200);
							}, 2000);
						}
					});

				} else {
					$('#payWay').addClass("payWay");
					setTimeout(function() {
						$('#payWay').removeClass("payWay");
					}, 2000);
				}
			} else {
				$('#alert-error').text('网络异常!');
				$('#alert-error').slideToggle(100);
				setTimeout(function() {
					$('#alert-error').slideUp(200);
				}, 2000);
			}
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {
		}
	});

}

//确认支付
$("#confirmSaleList").click(function(){
	
	if($("#saleName").text()==""||$("#saleAddress").text()==""||$("#salePhone").text()==""){
		$('#alert-error').text('请填写收货信息!');
		$('#alert-error').slideToggle(100);
		setTimeout(function(){$('#alert-error').slideUp(200);}, 2000);
		$("#addrPhone").focus();
		return;
	}else{
		var saleListUnique=getCookie("saleListUnique");//订单唯一性标识
		var saleListAddress=$("#saleName").text()+";"+$("#saleAddress").text()+";"+$("#salePhone").text();//收货人地址
		var saleListPayment=$("#selectPayment option:selected").val();//选择的支付方式
		var payMoney=$("#payTotals").text();//付款金额总价
		var shopUnique=getCookie("shopUnique");//店铺的唯一性标识
		var cusUnique = getCookie("cusId");//用户的唯一性标识
		var shoppingCartId=getCookie("basketIds");//购物车id
		var goodsCount=getCookie("goodsCount");//商品的数量
		var goodsBarcodes=getCookie("goodsBarcodes");//商品的条形码
		if(saleListPayment==1){//支付宝支付
			
			parent.location.href="../../payOnline/pay.do?payMoney="+payMoney+"&saleListAddress="+saleListAddress+"&" +
			"cusUnique="+cusUnique +"&shopUnique="+shopUnique+"&saleListUnique="+saleListUnique+"&shoppingCartId="+
			shoppingCartId+"&goodsCount="+goodsCount+"&goodsBarcodes="+goodsBarcodes;
			
		}else if(saleListPayment==2){//微信支付
		}else{//货到付款
			
			$.ajax({
				url : "../../payOnline/cashPay.do",
				type : "post",
				data : {"payMoney":payMoney,"saleListAddress":saleListAddress,"cusUnique":cusUnique,"shopUnique":shopUnique,
					"saleListUnique":saleListUnique,"shoppingCartId":shoppingCartId,"goodsCount":goodsCount,"goodsBarcodes":goodsBarcodes},
				dataType : "json",
				success : function(result) {
					if (result.status == 1) {// 查询成功
						parent.location.href="pay_finish.html";
					} else {
						$('#alert-error').text('网络异常!');
						$('#alert-error').slideToggle(100);
						setTimeout(function(){$('#alert-error').slideUp(200);}, 2000);
					}
				},
				error : function(XMLHttpRequest, textStatus, errorThrown) {
					$('#alert-error').text('网络异常!');
					$('#alert-error').slideToggle(100);
					setTimeout(function(){$('#alert-error').slideUp(200);}, 2000);
				}
			});
			
		}
		
	}
});









